﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.Collections;

namespace CitrixPVSWrapper
{

    [System.Management.Automation.Cmdlet("Run", "PvsCreateDisk", SupportsShouldProcess = true)]
    public class Run_PvsCreateDisk : System.Management.Automation.PSCmdlet
    {
        [Parameter(Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public String Name;

        [Parameter(Mandatory = true)]
        [ValidateRange(1,2088960)]
        public Int32 Size;

        [Parameter(Mandatory = false)]
        public PvsStore Store;

        [Parameter(Mandatory = false)]
        public String StoreId;

        [Parameter(Mandatory = false)]
        public String StoreName;

        [Parameter(Mandatory = false)]
        public PvsSite Site;

        [Parameter(Mandatory = false)]
        public String SiteId;

        [Parameter(Mandatory = false)]
        public String SiteName;

        [Parameter(Mandatory = false)]
        public PvsServer Server;

        [Parameter(Mandatory = false)]
        public String ServerId;

        [Parameter(Mandatory = false)]
        public String ServerName;

        [Parameter(Mandatory = false)]
        public String Description;

        [Parameter(Mandatory = false)]
        public Boolean Enabled = true;

        [Parameter(Mandatory = false)]
        [ValidateSet("Fixed", "Dynamic")]
        public String Type = "Fixed";

        [Parameter(Mandatory = false)]
        [ValidateSet("512", "2048", "16384")]
        public String VhdBlockSize = "2048";

        protected override void BeginProcessing()
        {
            // Test for mutually exclusive parameters
            Validate.MutuallyExclusiveParameters(this.MyInvocation.BoundParameters, new string[] { "Store", "StoreId", "StoreName" }, true);
            Validate.MutuallyExclusiveParameters(this.MyInvocation.BoundParameters, new string[] { "Site", "SiteId", "SiteName" }, true);
            Validate.MutuallyExclusiveParameters(this.MyInvocation.BoundParameters, new string[] { "Server", "ServerId", "ServerName" }, false);
        }

        protected override void ProcessRecord()
        {
            // Verbose and Debug Settings
            Boolean Verbose = this.MyInvocation.BoundParameters.ContainsKey("Verbose");
            WriteVerbose("Verbose Flag: " + Verbose);
            Boolean Debug = this.MyInvocation.BoundParameters.ContainsKey("Debug");
            WriteDebug("Debug Flag: " + Debug);

            // Collections to hold the queries and the results
            string TargetObject = "CreateDisk";

            //Build the query object
            QueryDefinition RunQuery = new QueryDefinition(QueryActionType.Run, TargetObject);

            // Add in the mandatory parameters
            RunQuery.Parameters.Add("name=\"" + this.Name + "\"");
            RunQuery.Parameters.Add("size=\"" + this.Size + "\"");

            // Add in the optional parameters
            if (MyInvocation.BoundParameters.ContainsKey("Store")) { RunQuery.Parameters.Add("storeId=\"" + this.Store.StoreId + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("StoreId")) { RunQuery.Parameters.Add("storeId=\"" + this.StoreId + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("StoreName")) { RunQuery.Parameters.Add("storeName=\"" + this.StoreName + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("Site")) { RunQuery.Parameters.Add("siteId=\"" + this.Site.SiteId + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("SiteId")) { RunQuery.Parameters.Add("siteId=\"" + this.SiteId + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("SiteName")) { RunQuery.Parameters.Add("siteName=\"" + this.SiteName + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("Server")) { RunQuery.Parameters.Add("serverId=\"" + this.Server.ServerId + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("ServerId")) { RunQuery.Parameters.Add("serverId=\"" + this.ServerId + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("ServerName")) { RunQuery.Parameters.Add("serverName=\"" + this.ServerName + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("Description")) { RunQuery.Parameters.Add("description=\"" + this.Description + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("Enabled")) { RunQuery.Parameters.Add("enabled=\"" + Convert.ToInt32(this.Enabled) + "\""); };
            if (MyInvocation.BoundParameters.ContainsKey("Type") && this.Type == "Dynamic") { RunQuery.Parameters.Add("type=1"); };
            if (MyInvocation.BoundParameters.ContainsKey("VhdBlockSize")) { RunQuery.Parameters.Add("vhdBlockSize=\"" + this.VhdBlockSize + "\""); };

            string ShouldProcessMessage = String.Format("Create a new disk with name {0} and size {1}Mb", this.Name, this.Size);
           
            // Perform the actual Run and return any errors
            if (ShouldProcess(ShouldProcessMessage))
            {
                foreach (string[] Record in PvsHelpers.ExecuteMcliQuery(RunQuery, Verbose, Debug))
                {
                    WriteDebug("Returning Object");
                    WriteObject(PvsHelpers.Get_StringValueFromRecord("GUID", Record, Verbose));
                }
            }
        }
    }
}
